package collision;



import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.util.ArrayList;


public class Craft extends Thing{

    private static String iiString = "guys.png";//"images"+ File.separator + "guys.png";


    private int dx;
    private int dy;
    private int power;
    int powerConstant = 50;
    private int maxPower = -8;
    private boolean powerBar;
    private int arrowsFired;
    private boolean doubleFire;
    private ArrayList<Missile> missiles;
    private Vector2D armVector;
    private int frameCount;
    private int lastFrameFired;
    private boolean fastFire;

    /**
     * Craft Constructor
     */
    public Craft() {
    	
    	super(150, 400, iiString);
    	

    	doubleFire = false;
    	fastFire = false;
    	armVector = new Vector2D(5,-5);
    	power = 0;
        missiles = new ArrayList<Missile>();
        arrowsFired = 0;
        powerBar=true;

    }

    /**
     * This method runs each frame to move the craft to its new position
     */
    public void move() {
    	addFrame();
    	int x = super.getX();
    	int y = super.getY();
        x += dx;
    	if(y<=300)
    		y = 301;
    	else if(y>=450)
    		y = 449;
    	else
    		y += dy;

        if (x < 1) {
            x = 1;
        }

        if (y < 1) {
            y = 1;
        }
        
        super.setPosition(x, y);
    }

    /**
     * Returns the current power that the craft has to fire an arrow.
     * 
     * @return  craft fire power
     */
    public int getPower() {
    	if(clickStartTime != 0)
    	{
    		int time  = (int) System.currentTimeMillis();
    		power = (clickStartTime - time)/powerConstant;
    	}
    	else
    		power=0;
    	if(-power>-maxPower)
    		return maxPower;
    	else
    		return power;
    }
    
    /**
     * Returns the max power constant of this craft.
     *
     * @return  maximum power
     */
    public int getMaxPower() {
        return maxPower;
    }
    
    /**
     * Adds to the frame count.
     */
    public void addFrame()
    {
    	frameCount++;
    }
    
    /**
     * Returns the vector of the last arrow that the craft fired which is its arm vector.
     *
     * @return  Vector2D that the crafts arm us pointing at
     */
    public Vector2D getArmVector()
    {
    	return armVector;
    }

    /**
     * The total number of arrow that the craft has fired.
     *
     * @return  total number of arrow fired
     */
    public int getArrowsFired() {
        return arrowsFired;
    }
    
    /**
     * Sets weather or not the craft has a power bar
     *
     * @param  b  true if the craft has a power bar false if it doesnt
     */
    public void hasPowerBar(boolean b)
    {
    	powerBar = b;
    }
    
    /**
     * Returns weather or not the craft uses a power bar
     *
     * @return  returns true if the craft uses its power bar and false if it doesn't.
     */
    public boolean usesPowerBar()
    {
    	return powerBar;
    }    
    
    /**
     * Sets weather or not the craft can double fire arrows
     *
     * @param  b  true if the craft can double fire false if it can't
     */
    public void setDoubleFire(boolean b)
    {
    	doubleFire = b;
    }
    
    /**
     * Returns weather or not the craft can double fire
     *
     * @return  returns true if the craft can double fire and false if it can't.
     */
    public boolean canDoubleFire()
    {
    	return doubleFire;
    }
    
    /**
     * Sets weather or not the craft can quick fire arrows
     *
     * @param  b  true if the craft can quick fire false if it can't
     */
    public void setQuickFire(boolean b)
    {
    	fastFire = b;
    }
    
    /**
     * Returns weather or not the craft can quick fire
     *
     * @return  returns true if the craft can quick fire and false if it can't.
     */
    public boolean getQuickFire()
    {
    	return fastFire;
    }
    

    /**
     * Returns returns an array list of all of the crafts current missiles.
     *
     * @return  ArrayList of Missile.
     */
    public ArrayList<Missile> getMissiles() {
        return missiles;
    }
    

    /**
     * Runs every time the user presses a keyboard key.
     *
     * @param e  The key event
     */
    public void keyPressed(KeyEvent e) {

        int key = e.getKeyCode();


        
        if (key == KeyEvent.VK_LEFT  ||  key == KeyEvent.VK_A) {
            dx = -1;
        }

        if (key == KeyEvent.VK_RIGHT  ||  key == KeyEvent.VK_D) {
            dx = 1;
        }

        if (key == KeyEvent.VK_UP  ||  key == KeyEvent.VK_W) {
            dy = -1;
        }

        if (key == KeyEvent.VK_DOWN  ||  key == KeyEvent.VK_S) {
            dy = 1;
        }
    }
    

    int clickStartTime;
    int clickEndTime;

    /**
     * Runs every time the user presses the mouse.
     *
     * @param  e  The mouse event
     */
    public void mousePressed(MouseEvent e) {

    	clickStartTime = (int) System.currentTimeMillis();

       
    }
    
    /**
     * Runs every time the user releases the mouse.
     *
     * @param  e  The mouse event
     */
    public void mouseReleased(MouseEvent e) 
    {
        //frameCount++;

    	
    	int x = super.getX();
    	int y = super.getY();
    	int width = super.getWidth();
    	int height = super.getHeight();

    	clickEndTime = (int) System.currentTimeMillis();
    	int t = (int)clickStartTime - clickEndTime;
    	int Xcomp = (x + width/2) - e.getX();
    	int Ycomp = e.getY()-(y + height/2);
    	power = maxPower;
    	power = (t)/powerConstant;
    	clickStartTime = 0;
    	if(power>=-1)
    		power=-2;
    	
        if(!powerBar || power < maxPower)
        	power = maxPower;
        
        int fireRate = 20;
        if(fastFire)
        	fireRate=-1;
        
    	if(frameCount-lastFrameFired>fireRate)
    	{
    		Vector2D v = new Vector2D(Xcomp, -Ycomp);
            v = v.normalize();
            v = v.mul(power);
            fire(v);    
            //armVector = v;
            if(doubleFire)
            {
                v = new Vector2D(Xcomp, -Ycomp-4);
                v = v.normalize();
                v = v.mul(power-.5);
                fire(v);      
            }
    	}

    }
    
    
    
    /**
     * Fires a missile at the vector in the parameter with 
     * the power set as the magnitude of the vector.
     *
     * @param  vect  Vector2D that the missile fires at
     */
    public void fire(Vector2D vect) {
    		int x = super.getX();
        	int y = super.getY();
        	int width = super.getWidth();
        	int height = super.getHeight();
        	armVector = vect;
            missiles.add(new Missile(x + width/2, y + height/2, vect));
            lastFrameFired = frameCount;
            arrowsFired++;
    	      
    }
    

    
    /**
     * Runs every time the user releases a keyboard key.
     *
     * @param e  The key event
     */
    public void keyReleased(KeyEvent e) {
        int key = e.getKeyCode();

        if (key == KeyEvent.VK_LEFT  ||  key == KeyEvent.VK_A) {
            dx = 0;
        }

        if (key == KeyEvent.VK_RIGHT  ||  key == KeyEvent.VK_D) {
            dx = 0;
        }

        if (key == KeyEvent.VK_UP  ||  key == KeyEvent.VK_W) {
            dy = 0;
        }

        if (key == KeyEvent.VK_DOWN  ||  key == KeyEvent.VK_S) {
            dy = 0;
        }
    }
}
